package com.wy.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.sun.org.apache.xpath.internal.operations.Mod;
import com.sun.org.apache.xpath.internal.operations.Or;
import com.wy.bean.Doctor;
import com.wy.bean.Keshi;
import com.wy.bean.Order;
import com.wy.bean.User;
import com.wy.service.UserService;
import com.wy.utils.MD5Utils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class UserController {
    @Resource
    private UserService userService;
    @RequestMapping("loginlist")
    public String loginlist(User user, Model model){
        List<User> list = userService.loginlist(user);
        return "loginlist";
    }
    @RequestMapping("login")
    @ResponseBody
    public boolean login(Model model, User user, HttpSession session){
        List<User> users = userService.loginlist(user);
        if(users.size()>0){
            session.setAttribute("users",users.get(0));
            return true;
        }else {
            return false;
        }
    }
    @RequestMapping("list")
    public String list(Model model,String kid,
                       @RequestParam(defaultValue = "1") int pageNum,
                       @RequestParam(defaultValue = "2") int pageSize
                       ){
        List<Keshi> list = userService.list();
        model.addAttribute("list",list);
        if(StringUtil.isNotEmpty(kid)){
            PageHelper.startPage(pageNum,pageSize);
            List<Doctor> doctorlist = userService.getDoctorlist(kid);
            PageInfo<Doctor> pageInfo = new PageInfo<>(doctorlist);
            model.addAttribute("pageInfo",pageInfo);
            model.addAttribute("kid",kid);
        }
        return "list";
    }
    @RequestMapping("add")
    public String add(){
        return "add";
    }

    @RequestMapping("save")
    @ResponseBody
    public Boolean save(User user,Model model){
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String date = format.format(new Date());
            user.setCreatetime(date);
            String password = user.getPassword();
            String encode = MD5Utils.encode(password);
            user.setPassword(encode);
            userService.insert(user);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    @RequestMapping("yuyue")
    @ResponseBody
    public Boolean yuyue(String did,Model model,HttpSession session){
        try {
            Order order = new Order();
            User users = (User) session.getAttribute("users");
            order.setUid(users.getUid());
            order.setDid(did);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String format = simpleDateFormat.format(new Date());
            order.setCreate_time(format);
            userService.insertbth(order);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }
    @RequestMapping("aayuyue")
    public String aayuyue(String uid,HttpSession session,Model model){
        User users = (User) session.getAttribute("users");
        List<Order> list = userService.getOrderlist(users.getUid());
        model.addAttribute("list",list);
        return "aayuyue";
    }

}
